import json
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from apps.app.models import *

# 通用
class CommonSerializer(serializers.ModelSerializer):
    def xTree(datas):
        lists = []
        tree = {}
 
        for i in datas:
            item = i
            tree[item['id']] = item
 
        for i in datas:
            obj = i
            if not obj['pid']:
                root = tree[obj['id']]
                lists.append(root)
            else:
                parent_id = obj['pid']
                if 'children' not in tree[parent_id]:
                    tree[parent_id]['children'] = []
                tree[parent_id]['children'].append(tree[obj['id']])
        return lists

# 用户
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'url', 'username', 'email', 'groups']

# 分组
class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ['id', 'url', 'name']

# 菜单
class MenuSerializer(serializers.ModelSerializer):
    class Meta:
        model = MenuModel
        fields = ['id', 'name', 'pid', 'order_num', 'path', 'component', 'query', 
                  'links', 'cache', 'type', 'visible', 'status', 'perms', 'icon', 
                  'create_time', 'update_time']
    
# 菜单
class MenuTreeSerializer(serializers.ModelSerializer):

    #value = serializers.SerializerMethodField(source='id')
    #label = serializers.SerializerMethodField(source='name')
    class Meta:
        model = MenuModel
        fields = ['id', 'name', 'pid', 'order_num', 'path']
        #fields = ['value', 'label', 'pid', 'order_num', 'path']
 
# 单位
class DeptSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeptModel
        fields = ['id', 'name', 'pid', 'order_num', 'type',  
                  'create_time', 'update_time']
        
# 单位
class DeptTreeSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeptModel
        fields = ['id', 'name', 'pid', 'order_num', 'type']
        
# 角色
class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = RoleModel
        fields = ['id', 'name', 'order_num', 
                  'create_time', 'update_time']
        
# 人员管理
class StaffSerializer(serializers.ModelSerializer):
    class Meta:
        model = StaffModel
        fields = ['id', 'name', 'dept_id', 'idcard', 'mobile', 'address', 'entry_time', 
                  'leave_time', 'job', 
                  'create_time', 'update_time']
        
# 终端管理
class DeviceSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeviceModel
        fields = ['id', 'dept_id', 'staff_id', 'mac', 'ip', 'sys',  
                  'create_time', 'update_time']

# 终端检查管理
class DeviceCheckSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeviceCheckModel
        fields = ['id', 'device_id', 'mac_id', 
                  'create_time', 'update_time']
                
# 终端检查管理-关联表
class DeviceCheckAllSerializer(serializers.ModelSerializer):
    deviceCheck = serializers.SerializerMethodField()
    def get_deviceCheck(self, obj):
        curDevice = obj
        deviceCheck = DeviceCheckModel.objects.filter(device_id = curDevice.id)
        courses_serializer = DeviceCheckSerializer(deviceCheck, many=True)
        return courses_serializer.data #json.dumps(list(device))
    
    class Meta:
        model = DeviceModel
        fields = '__all__' #['id', 'device_id', 'mac_id', 'dept_id', 'staff_id', 'mac', 'ip', 'sys', 'create_time', 'update_time']
        read_only_fields = ['deviceCheck'] 
        
# 终端检查管理
class DeviceSoftSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeviceSoftModel
        fields = '__all__'
        
# 合同管理
class AgreementSerializer(serializers.ModelSerializer):
    class Meta:
        model = AgreementModel
        fields = ['id', 'name', 'no', 'first_party', 'second_party', 'effective_time', 'links', 
                  'create_time', 'update_time']
        
# 授权管理
class AuthSerializer(serializers.ModelSerializer):
    class Meta:
        model = AuthModel
        fields = ['id', 'agreement_id', 'dept_id', 'name', 'type', 'version', 'total', 
                  'num', 'validity_time', 'is_xc', 
                  'create_time', 'update_time']
        
# 领导小组
class LeaderSerializer(serializers.ModelSerializer):
    class Meta:
        model = LeaderModel
        fields = ['id', 'dept_id', 'name', 'job', 'title', 'mobile', 'phone', 
                  'remarks', 
                  'create_time', 'update_time']
        
# 资产申请记录表
class ApplySerializer(serializers.ModelSerializer):
    class Meta:
        model = ApplyModel
        fields = ['id', 'dept_id', 'staff_id', 'type', 'purpose', 'fun', 'num', 
                  'remarks',  
                  'create_time', 'update_time']
        
# 采购计划表
class PurchaseSerializer(serializers.ModelSerializer):
    class Meta:
        model = PurchaseModel
        fields = ['id', 'dept_id', 'staff_id', 'type', 'is_xc', 'name', 'version', 
                  'num', 'validity_time', 'price', 'total', 'remarks', 
                  'create_time', 'update_time']
        

# 相关制度
class DutySerializer(serializers.ModelSerializer):
    class Meta:
        model = DutyModel
        fields = ['id', 'name', 'links', 'remarks', 
                  'create_time', 'update_time']
        
# 资产列表
class HardwareSerializer(serializers.ModelSerializer):
    class Meta:
        model = HardwareModel
        fields = ['id', 'agreement_name', 'agreement_time', 'dept_id', 'brand', 'version', 'device_id', 
                  'ip', 'remarks', 
                  'create_time', 'update_time']
        
# 采购管理
class HardwarePurchaseSerializer(serializers.ModelSerializer):
    class Meta:
        model = HardwarePurchaseModel
        fields = ['id', 'agreement_name', 'supplier_name', 'username', 'agreement_time', 'dept_id', 'price', 
                  'num', 'remarks', 
                  'create_time', 'update_time']
        
# 供应商管理
class SupplierSerializer(serializers.ModelSerializer):
    class Meta:
        model = SupplierModel
        fields = ['id', 'name', 'username', 'mobile', 'address', 'join_time', 'remarks', 
                  'create_time', 'update_time']      
          
# 字典管理
class DictSerializer(serializers.ModelSerializer):
    class Meta:
        model = DictModel
        fields = '__all__'

# 字典值管理
class DictDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = DictDataModel
        fields = '__all__'

# 字典管理-关联表
class DictAllSerializer(serializers.ModelSerializer):
    dictData = serializers.SerializerMethodField()
    def get_dictData(self, obj):
        cur = obj
        dictData = DictDataModel.objects.filter(type = cur.type)
        serializer = DictDataSerializer(dictData, many=True)
        return serializer.data #json.dumps(list(device))
    
    class Meta:
        model = DictModel
        fields = '__all__' #['id', 'device_id', 'mac_id', 'dept_id', 'staff_id', 'mac', 'ip', 'sys', 'create_time', 'update_time']
        read_only_fields = ['dictData'] 
             
    def toDict(datas):
        obj = {}
        for item in datas:
            dictData = item['dictData']
            list = []
            for data in dictData:
                list.append({'value': data['id'], 'label': data['label']})
            obj[item['type']] = list

        return obj